package com.iotp.controller.zb;

import com.alibaba.fastjson.JSONObject;
import com.iotp.entity.base.DataMessage;
import com.iotp.entity.expert.Expert;
import com.iotp.entity.live.Live;
import com.iotp.service.expert.ExpertService;
import com.iotp.service.live.LiveService;
import com.iotp.service.userUserRef.UserUserRefService;
import com.iotp.utils.zb.ChannelManagementUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 直播
 */
@RestController
@RequestMapping("/zb")
public class ZbController {

    @Autowired
    private LiveService liveService;
    @Autowired
    private ExpertService expertService;
    @Autowired
    private UserUserRefService userUserRefService;

    @PostMapping("/createChannel")
    public DataMessage createChannel(String channelName, String userId){
        DataMessage dm = new DataMessage();
        if(StringUtils.isEmpty(channelName) || StringUtils.isEmpty(userId)){
            dm.setResult(1);
            dm.setData(null);
            dm.setMessage("请输入名称");
        }else{
            JSONObject json = ChannelManagementUtil.createChannel(channelName+"_"+System.currentTimeMillis(), 0);
            if(json != null && !json.isEmpty()){
                Integer code = json.getInteger("code");
                if(code == 200){
                    JSONObject ret = JSONObject.parseObject(json.getString("ret"));
                    Live add = new Live();
                    add.setExpertId(userId);
                    add.setChannelName(ret.getString("name"));
                    add.setcId(ret.getString("cid"));
                    add.setPushUrl(ret.getString("pushUrl"));
                    add.setHttpPullUrl(ret.getString("httpPullUrl"));
                    add.setHlsPullUrl(ret.getString("hlsPullUrl"));
                    add.setRtmpPullUrl(ret.getString("rtmpPullUrl"));
                    add.setCreateTime(System.currentTimeMillis());
                    Expert e = this.expertService.queryByUserId(userId);
                    add.setHeadImgUrl(e==null?null:e.getHeadImgUrl());
                    Long id = this.liveService.add(add);
                    dm.setResult(0);
                    add.setStatus(1);
                    dm.setData(add);
                    dm.setMessage("创建成功");
                }else{
                    dm.setResult(1);
                    dm.setData(null);
                    dm.setMessage(json.getString("msg"));
                }
            }else{
                dm.setResult(1);
                dm.setData(null);
                dm.setMessage("创建失败");
            }
        }
        return dm;
    }
    @PostMapping("/getChannelById")
    public DataMessage getChannelById(String id){
        DataMessage dm = new DataMessage();
        if(StringUtils.isEmpty(id)){
            dm.setResult(1);
            dm.setData(null);
            dm.setMessage("频道id为空");
        }else{
            Live l = this.liveService.queryById(Long.parseLong(id));

            dm.setResult(0);
            dm.setData(l);
            dm.setMessage("查询成功");
        }
        return dm;
    }

    @PostMapping("/getChannelList")
    public DataMessage getChannelList(){
        DataMessage dm = new DataMessage();
        List<Live> list = this.liveService.queryList(new Live());
        if(list == null || list.isEmpty()){
            dm.setResult(1);
            dm.setData(null);
            dm.setMessage("没有直播");
        }else{
            for(Live l : list){
                JSONObject json = ChannelManagementUtil.getStatsChannel(l.getcId());
                //0：空闲； 1：直播； 2：禁用； 3：直播录制
                if(json.getInteger("code") == 200){
                    l.setStatus(JSONObject.parseObject(json.getString("ret")).getInteger("status"));
                }else{
                    l.setStatus(0);
                }
            }
            dm.setResult(0);
            dm.setData(list);
            dm.setMessage("查询成功");
        }
        return dm;
    }
}
